//
//  myBestTableViewCell.m
//  test-git
//
//  Created by IOS_wt on 15/12/2.
//  Copyright © 2015年 wangteng. All rights reserved.
//

#import "myBestTableViewCell.h"

@implementation myBestTableViewCell

{
    NSArray* _imageConstraintsH;
    NSArray* _imageConstraintsV;
    
    NSArray* _labelConstraintsH;
    NSArray* _labelConstraintsV;
    NSArray* _buttonConstraintsH;
    NSArray* _buttonConstraintsV;
    
    NSDictionary* dic;
    
}

- (void)awakeFromNib {
    // Initialization code
    
    self.selectionStyle=UITableViewCellSelectionStyleNone;
    
    self->_mImage=[[UIImageView alloc]init];
    self->_mLabel=[[UILabel alloc]init];
    self->_mButton=[[UIButton alloc]init];
    
    [self addSubview:self->_mImage];
    [self addSubview:self->_mLabel];
    [self addSubview:self->_mButton];
    self->_mImage.translatesAutoresizingMaskIntoConstraints=NO;
    self->_mLabel.translatesAutoresizingMaskIntoConstraints=NO;
    self->_mButton.translatesAutoresizingMaskIntoConstraints=NO;
    self->_mLabel.lineBreakMode=NSLineBreakByCharWrapping;
    self->_mLabel.numberOfLines=0;
    self->_mImage.userInteractionEnabled=YES;
    self->_mImage.backgroundColor=[UIColor greenColor];
    
  self->_mImage.layer.cornerRadius=self->_mImage.frame.size.height/2;
  self->_mImage.clipsToBounds=YES;

    
    self->_mButton.backgroundColor=[UIColor cyanColor];
    [self->_mButton setTitle:@"确定" forState:UIControlStateNormal];
    [self->_mButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    
    [self->_mButton sizeToFit];
    [self->_mButton addTarget:self action:@selector(ok) forControlEvents: UIControlEventTouchUpInside];
    
    dic=NSDictionaryOfVariableBindings(_mImage,_mLabel,_mButton);
    
    self->_imageConstraintsH=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_mImage(==100)]" options:0 metrics:nil views:dic];
    self->_imageConstraintsV=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_mImage]|" options:0 metrics:nil views:dic];
    [self addConstraints:self->_imageConstraintsH];
    [self addConstraints:self->_imageConstraintsV];
    
    self->_labelConstraintsH=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_mImage(==100)]-[_mLabel]|" options:0 metrics:nil views:dic];
    self->_labelConstraintsV=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_mLabel]" options:0 metrics:nil views:dic];
    [self addConstraints:self->_labelConstraintsH];
    [self addConstraints:self->_labelConstraintsV];
    
    self->_buttonConstraintsH=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_mButton]|" options:0 metrics:nil views:dic];
    self->_buttonConstraintsV=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_mLabel]-[_mButton]|" options:0 metrics:nil views:dic];
    [self addConstraints:self->_buttonConstraintsH];
    [self addConstraints:self->_buttonConstraintsV];
    
    
    
}
-(void)ok{
    [self removeConstraints:self->_labelConstraintsV];
    [self removeConstraints:self->_labelConstraintsH];
    [self removeConstraints:self->_imageConstraintsV];
    [self removeConstraints:self->_imageConstraintsH];
    
    dic=NSDictionaryOfVariableBindings(_mImage,_mLabel,_mButton);
    //创建动画并指定动画属性
    CABasicAnimation *basicAnimation=[CABasicAnimation animationWithKeyPath:@"position"];
    
    //设置其他动画属性
    basicAnimation.duration=3.0;//动画时间3秒
       //添加动画到图层，注意key相当于给动画进行命名，以后获得该动画时可以使用此名称获取
    [self->_mImage.layer addAnimation:basicAnimation forKey:@"KCBasicAnimation_Translation"];
    [self->_mLabel.layer addAnimation:basicAnimation forKey:@"KCBasicAnimation_Translation"];
    
    self->_imageConstraintsH=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_mImage(==100)]|" options:0 metrics:nil views:dic];
    self->_imageConstraintsV=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_mImage]-[_mButton]|" options:0 metrics:nil views:dic];
    [self addConstraints:self->_imageConstraintsH];
    [self addConstraints:self->_imageConstraintsV];
    
    self->_labelConstraintsH=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_mLabel]-[_mImage(==100)]" options:0 metrics:nil views:dic];
    self->_labelConstraintsV=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_mLabel]-[_mButton]" options:0 metrics:nil views:dic];
    [self addConstraints:self->_labelConstraintsH];
    [self addConstraints:self->_labelConstraintsV];
    [self->_mButton addTarget:self action:@selector(changed) forControlEvents:UIControlEventTouchUpInside];
    
}
-(void)changed{
    [self removeConstraints:self->_labelConstraintsV];
    [self removeConstraints:self->_labelConstraintsH];
    [self removeConstraints:self->_imageConstraintsV];
    [self removeConstraints:self->_imageConstraintsH];

    dic=NSDictionaryOfVariableBindings(_mImage,_mLabel,_mButton);
    
    self->_imageConstraintsH=[NSLayoutConstraint constraintsWithVisualFormat:@"H:|[_mImage(==100)]" options:0 metrics:nil views:dic];
    self->_imageConstraintsV=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_mImage]|" options:0 metrics:nil views:dic];
    [self addConstraints:self->_imageConstraintsH];
    [self addConstraints:self->_imageConstraintsV];
    
    self->_labelConstraintsH=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_mImage(==100)]-[_mLabel]|" options:0 metrics:nil views:dic];
    self->_labelConstraintsV=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_mLabel]" options:0 metrics:nil views:dic];
    [self addConstraints:self->_labelConstraintsH];
    [self addConstraints:self->_labelConstraintsV];
    
    self->_buttonConstraintsH=[NSLayoutConstraint constraintsWithVisualFormat:@"H:[_mButton]|" options:0 metrics:nil views:dic];
    self->_buttonConstraintsV=[NSLayoutConstraint constraintsWithVisualFormat:@"V:|[_mLabel]-[_mButton]|" options:0 metrics:nil views:dic];
    [self addConstraints:self->_buttonConstraintsH];
    [self addConstraints:self->_buttonConstraintsV];

    
    
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
